package com.qs.eggs.view.fragment;

import android.content.Intent;
import android.support.v7.widget.SearchView;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.qs.eggs.R;
import com.qs.eggs.adapter.SortAdapter;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.bean.SortBean;
import com.qs.eggs.refresh.adapter.BaseRecyclerAdapter;
import com.qs.eggs.refresh.fragments.BaseRecyclerViewFragment;
import com.qs.eggs.view.activity.SortActivity;
import com.qs.eggs.view.prodetail.activity.ProDetailActivity;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class XiaoLiangSortFragment extends BaseRecyclerViewFragment<SortBean> implements
        SearchView.OnQueryTextListener{
    private String biaoshi;
    SearchView searchView;
    @Override
    protected BaseRecyclerAdapter<SortBean> getRecyclerAdapter() {
        return new SortAdapter(getActivity(), BaseRecyclerAdapter.ONLY_FOOTER);
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        biaoshi=getActivity().getIntent().getStringExtra("biaoshi");
        searchView= (SearchView) SortActivity.instance.findViewById(R.id.poi_et_search);
        searchView.setOnQueryTextListener(this);

    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void onItemClick(int position, long itemId) {
        super.onItemClick(position, itemId);
        SortBean sub = mAdapter.getItem(position);

        if (sub == null)
            return;
        Intent intent=new Intent(getActivity(),ProDetailActivity.class);
        intent.putExtra("proid",sub.getId());
        intent.putExtra("type",sub.getType());
        startActivity(intent);
    }

    @Override
    protected void requestData() {
        super.requestData();
        if(biaoshi.equals("like")){
            HttpApi.morePro(biaoshi,"xiaoliang",mHandler);
        }else if(getActivity().getIntent().getStringExtra("biaoshi").equals("search")&&searchView.getQuery().length()==0){
            HttpApi.searchPro(getActivity().getIntent().getStringExtra("pronameLIKE"),"xiaoliang",mHandler);
        }else if(searchView.getQuery().length()!=0) {
            isRefreshing = true;
            HttpApi.searchPro(searchView.getQuery().toString(), "xiaoliang", mHandler);
        }else if(biaoshi.equals("benner")){
            HttpApi.bennrtPro(getActivity().getIntent().getStringExtra("specialid"),"xiaoliang",mHandler);
        }else {
            HttpApi.morePro(biaoshi,"xiaoliang",mHandler);
        }

    }


    @Override
    protected Type getType() {
        return new TypeToken<ArrayList<SortBean>>() {
        }.getType();
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        requestData();
      //  HttpApi.searchPro(newText,"xiaoliang",mHandler);
        return false;
    }
}